package com.tz.cartelo.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.tz.cartelo.R;
import com.tz.cartelo.model.GoodsList;
import com.tz.cartelo.util.CommonUtils;

import java.util.List;


/**
 * Created by ok on 2016/9/18.
 */
public class MyOrderAdapter extends BaseAdapter {
    private LayoutInflater mLayoutInflater;
    private List<GoodsList> mGoodsListList;
    private Context mContext;
    private int mImgWidth, mImgHeight;

    public MyOrderAdapter(Context context, List<GoodsList> goodsListList) {
        mLayoutInflater = LayoutInflater.from(context);
        mContext = context;
        mGoodsListList = goodsListList;
        setImgSize();
    }

    private void setImgSize() {
        mImgWidth = CommonUtils.getScreenWidth(mContext) / 4;
        mImgHeight = mImgWidth;
        //+ mContext.getResources().getDimensionPixelOffset(R.dimen.margin_5)
    }

    @Override
    public int getCount() {
        return mGoodsListList.size();
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(final int i, View view, ViewGroup viewGroup) {
        ViewHolder holder;
//        if (view == null) {
        view = mLayoutInflater.inflate(R.layout.item_my_order, null);
        holder = new ViewHolder();
        holder.itemGoodsCenterImg = (ImageView) view.findViewById(R.id.item_goods_center_img);
        holder.itemManagerStockNameText = (TextView) view.findViewById(R.id.item_manager_stock_name_text);
        holder.itemManagerStockPriceText = (TextView) view.findViewById(R.id.item_manager_stock_price_text);
        holder.itemManagerStockNumText = (TextView) view.findViewById(R.id.item_manager_stock_num_text);
        holder.itemManagerStockStatusText = (TextView) view.findViewById(R.id.item_manager_stock_status_text);
        view.setTag(holder);
//        } else {
//            holder = (ViewHolder) view.getTag();
//        }

//        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(mImgWidth, mImgHeight);
//        holder.itemGoodsCenterImg.setLayoutParams(layoutParams);

//        ImageLoader.getInstance().displayImage("drawable://" + R.mipmap.ic_holder_list, holder.itemGoodsDetailsImg, CommonUtils.optionsList());
        ImageLoader.getInstance().displayImage(mGoodsListList.get(i).getGoodsPicture(), holder.itemGoodsCenterImg, CommonUtils.optionsList());
        holder.itemManagerStockNameText.setText(mGoodsListList.get(i).getTitle());
        holder.itemManagerStockPriceText.setText("¥" + mGoodsListList.get(i).getPrice());
        holder.itemManagerStockNumText.setText("下单总数：" + mGoodsListList.get(i).getSalesNum() + mContext.getResources().getString(R.string.goods_center_total_stock_unit));


        //0：下单中，1审核中，2交易成功，其他交易失败
        switch (mGoodsListList.get(i).getState()) {
            case "0":
                holder.itemManagerStockStatusText.setText("下单中");
                break;
            case "1":
                holder.itemManagerStockStatusText.setText("审核中");
                break;
            case "2":
                holder.itemManagerStockStatusText.setText("交易成功");
                break;
            default:
                holder.itemManagerStockStatusText.setText("交易失败");
                break;
        }

        return view;
    }

    private class ViewHolder {
        ImageView itemGoodsCenterImg;
        TextView itemManagerStockNameText;
        TextView itemManagerStockPriceText;
        TextView itemManagerStockNumText;
        TextView itemManagerStockStatusText;
    }

}
